	import com.adobe.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.TextInput;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.validators.NumberValidator;
	
	import pages.BaseSearchTO;
	import pages.common.PaginationHelper;
	import mx.events.ValidationResultEvent;
	
	[Bindable]
	public var page:Object;
	
	[Bindable]
	public var list:ArrayCollection;
	
	[Bindable]
	public var deleteList:ArrayCollection=new ArrayCollection();
	
	[Bindable]  
	public var helper:PaginationHelper=new PaginationHelper();
	
	public function initialPageInstance(object:Object):void{
		page=object;
	}

	public function getFirst(helper:PaginationHelper,searchTO:BaseSearchTO,obj:Object):void  {
		helper.start=0;
		obj.getSearchList(helper,searchTO);
	}
	
	public function getLast(helper:PaginationHelper,searchTO:BaseSearchTO,obj:Object):void {
		helper.start=(helper.getTotalPage()-1)*helper.objectPerPage;
		obj.getSearchList(helper,searchTO);
	}
				
	public function getNext(helper:PaginationHelper,searchTO:BaseSearchTO,obj:Object):void {
		if(hasNext(helper)){
			helper.start=helper.start+helper.objectPerPage;
			obj.getSearchList(helper,searchTO);
		}
	}
	
	public function hasNext(helper:PaginationHelper):Boolean{
		if (helper.getTotalPage() == helper.getCurrentPage()) {
			return false;
		} else if (helper.getTotalPage() <= 1) {
			return false;
		} else {
			return true;
		}
	}
	
	public function getPrev(helper:PaginationHelper,searchTO:BaseSearchTO,obj:Object):void {
		if(hasPrev(helper)){
			helper.start=helper.start-helper.objectPerPage;
			obj.getSearchList(helper,searchTO);
		}
	}
	
	public function hasPrev(helper:PaginationHelper):Boolean {
		if (helper.start == 0) {
			return false;
		} else {
			return true;
		}
	}
			
	public function getGo(event:KeyboardEvent,goNumber:TextInput,helper:PaginationHelper,searchTO:BaseSearchTO,obj:Object):void {
		var errMsg:String="请输入一个整数";
		if (event.charCode == 13){
			var page:String=goNumber.text;
			if(StringUtil.stringHasValue(StringUtil.trim(page))){
				var v:NumberValidator=new NumberValidator();
				v.domain="int";
				v.source=goNumber;
				v.property="text";
				v.integerError=errMsg;
				v.allowNegative=false;
				v.invalidCharError=errMsg;
				v.invalidFormatCharsError=errMsg;
				var vResult:ValidationResultEvent = v.validate(goNumber.text);
 				if (vResult.type==ValidationResultEvent.VALID) {
 					var p:Number=new Number(page);
					var start:Number = (p-1)*helper.objectPerPage;
					if(start<0)start=0;
					var lastPageStart:Number=(helper.getTotalPage()-1)*helper.objectPerPage;
					if(start>lastPageStart)start=lastPageStart;
					helper.start=start;
					obj.getSearchList(helper,searchTO);
            	}else {
                 	getDefaultAlertMsg(errMsg);
                }
			}else{
				getDefaultAlertMsg(errMsg);
			}
		}
	}

	public function labFunc(item:Object, column:DataGridColumn):String {
		return (((list.getItemIndex(item)+1)+(helper.getCurrentPage()-1)*helper.objectPerPage).toString());
	}
	
	public function getDeleteList():void{
		deleteList=new ArrayCollection();
        for(var i:int=0;i<list.length;i++){
            if(list.getItemAt(i).selected){   
                deleteList.addItem(list.getItemAt(i));   
            }   
        }
	}
	
	public function needAtLeastOneRecord():void{
		getDefaultAlertMsg("请至少选择一条数据!");
	}
	

	
	public function onlyOneRecordSelected(dataList:ArrayCollection):Number{
		var count:int=0;
    	var index:int=0;
        for(var i:int=0;i<dataList.length;i++){
            if(dataList.getItemAt(i).selected){   
                count++; 
                index=i;  
            }   
        }
        if(count==1){
        	return index;
        }else{
        	return -1;
        }
	}